package com.face.server.service.impl;

import com.face.server.dao.JoinedDAO;
import com.face.server.dao.VisitorDAO;
import com.face.server.entity.Admin;
import com.face.server.entity.DormitoryBuild;
import com.face.server.repository.DormitoryBuildRepository;
import com.face.server.repository.DormitoryRepository;
import com.face.server.repository.EventLogRepository;
import com.face.server.service.DormitoryBuildService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.security.Principal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * Created by ryan on 2018/3/17.
 */
@Service
public class DormitoryBuildServiceImpl implements DormitoryBuildService{

    @Autowired
    private JoinedDAO joinedDAO;

    @Autowired
    private DormitoryRepository dormitoryRepository;

    @Autowired
    private DormitoryBuildRepository dormitoryBuildRepository;

    @Autowired
    private VisitorDAO visitorDAO;

    @Autowired
    private EventLogRepository eventLogRepository;

    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Integer countStuInDorm() {
        Long count=joinedDAO.countByDorm();
        return Integer.parseInt(count.toString());
    }

    @Override
    public Integer countDormOccupied() {
        return Integer.parseInt(dormitoryRepository.countByStudentsNameExistsAndDisableIsFalse().toString());
    }

    @Override
    public Integer countVisitors() {
        Long count=visitorDAO.countVisitors();
        return Integer.parseInt(count.toString());
    }

    @Override
    public Page<DormitoryBuild> listDormBuildInfo(Pageable pageRequest, Integer buildNum) {
        List<DormitoryBuild> dormitoryBuilds=new ArrayList<DormitoryBuild>();
        Calendar startTime=Calendar.getInstance();
        Calendar endTime=Calendar.getInstance();
        startTime.set(Calendar.HOUR_OF_DAY,0);
        startTime.set(Calendar.MINUTE,0);
        startTime.set(Calendar.SECOND,0);
        endTime.set(Calendar.HOUR_OF_DAY,23);
        endTime.set(Calendar.MINUTE,59);
        endTime.set(Calendar.SECOND,59);
        Date start=startTime.getTime();
        Date end=endTime.getTime();
        if (buildNum==null||buildNum==0){
            dormitoryBuilds=dormitoryBuildRepository.findAll(pageRequest).getContent();
        }
        else
            dormitoryBuilds.add(dormitoryBuildRepository.findByBuildingNum(buildNum));
        if (dormitoryBuilds==null)
            return null;
        dormitoryBuilds.forEach(e->{
            e.setVisitorNum(visitorDAO.countVisitors(buildNum).intValue());
            e.setTodayStudentNum(eventLogRepository.countByBuildNumberIsAndTimeBetweenAndTypeIs(e.getBuildingNum(),start,end,0).intValue());
            e.setTodayVisitorNum(eventLogRepository.countByBuildNumberIsAndTimeBetweenAndTypeIs(e.getBuildingNum(),start,end,0).intValue());
        });
        return new PageImpl<DormitoryBuild>(dormitoryBuilds,pageRequest,dormitoryBuilds.size());
    }

    @Override
    public Page<DormitoryBuild> listDormBuildInfo(Pageable pageable, Integer buildNum, Principal principal) {
        Subject subject = SecurityUtils.getSubject();
        Admin admin = (Admin) subject.getPrincipal();
        List<Integer> dormitoryNumList=admin.getDormitoryNumList();
        List<DormitoryBuild> dormitoryBuilds=new ArrayList<DormitoryBuild>();
        Calendar startTime=Calendar.getInstance();
        Calendar endTime=Calendar.getInstance();
        startTime.set(Calendar.HOUR_OF_DAY,0);
        startTime.set(Calendar.MINUTE,0);
        startTime.set(Calendar.SECOND,0);
        endTime.set(Calendar.HOUR_OF_DAY,23);
        endTime.set(Calendar.MINUTE,59);
        endTime.set(Calendar.SECOND,59);
        Date start=startTime.getTime();
        Date end=endTime.getTime();
        if (buildNum==null||buildNum==0){
            dormitoryBuilds=dormitoryBuildRepository.findByBuildingNumIn(dormitoryNumList,pageable).getContent();
        }
        else
            dormitoryBuilds.add(dormitoryBuildRepository.findByBuildingNumInAndBuildingNumIs(dormitoryNumList,buildNum));
        if (dormitoryBuilds==null)
            return null;
        dormitoryBuilds.forEach(e->{
            e.setVisitorNum(visitorDAO.countVisitors(buildNum).intValue());
            e.setTodayStudentNum(eventLogRepository.countByBuildNumberIsAndTimeBetweenAndTypeIs(e.getBuildingNum(),start,end,0).intValue());
            e.setTodayVisitorNum(eventLogRepository.countByBuildNumberIsAndTimeBetweenAndTypeIs(e.getBuildingNum(),start,end,0).intValue());
        });
        return new PageImpl<DormitoryBuild>(dormitoryBuilds,pageable,dormitoryBuilds.size());
    }
}
